//
//  HierarchicalNamedObject.m
//  aPlanner
//
//  Created by Daniel Braun on 02/09/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "HierarchicalNamedObject.h"


@implementation HierarchicalNamedObject

- (void) dealloc {
	[name release];
	[super dealloc];
}

- (id) init {
	self = [super init];
	if (self != nil) {
		name=nil;
	}
	return self;
}

- (id) initWithName:(NSString *)s
{
	self=[self init];
	if (self) name=[s retain];
	return self;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
	// FLD
	[super encodeWithCoder:coder];
	if ( [coder allowsKeyedCoding] ) {
		[coder encodeObject:name forKey:@"name"];
	} else {
		NSAssert(0, @"only key coder supported\n");
	}
}


- (id) initWithCoder:(NSCoder *) coder
{
	self=[super initWithCoder:coder];
	if ([coder allowsKeyedCoding]) {
		name=[[coder decodeObjectForKey:@"name"]retain];
	}
	return self;
}

- (NSString *)name
{
	return name;
}
- (void) setName:(NSString *)n
{
	if (n != name) {
		[name release];
		name=[n retain];
	}
}
- (id) childNamed:(NSString *)s
{
	unsigned int i, count = [children count];
	for (i = 0; i < count; i++) {
		HierarchicalNamedObject * obj = [children objectAtIndex:i];
		if (![obj respondsToSelector:@selector(name)]) continue;
		if ([[obj name]isEqualToString:s]) return obj;
	}
	return nil;
}
- (id)valueForKey:(NSString *)key
{
	NSString *k=[key substringFromIndex:1];
	if ([key hasPrefix:@"!"]) {
		return [self childNamed:k];
	} else if ([key hasPrefix:@"+"]) {
		id s=[self recurseSelectorUntilNotil:@selector(childNamed:) withObject:k];
		return s;
	} else if ([key hasPrefix:@"#"]) {
		int z=[k intValue];
		if (z>=[children count]) return nil;
		return [children objectAtIndex:z];
	}
	id r= [super valueForKey:key];
	return r;
}



@end
